const app = getApp()
const QRCode = require('../../utils/weapp-qrcode.js')
import rpx2px from '../../utils/rpx2px.js'
let qrcode;
// 300rpx 在6s上为 150px
const qrcodeWidth = rpx2px(700)

Page({
  data: {
    meizi: {},
    qrcodeWidth: 700,
    isShowQrcode: false
  },
  onLoad() {
    const self = this
    this.getMeizi()
  },
  share() {
    const self = this
    self.setData({
      isShowQrcode: true
    })
  },
  close() {
    const self = this
    self.setData({
      isShowQrcode: false
    })
  },
  getMeizi() {
    const self = this
    app.http({
      url: 'https://gank.io/api/v2/random/category/Girl/type/Girl/count/1'
    }).then(res => {
      console.log(res.data[0])
      let tempData = res.data[0]
      let date = new Date(tempData.createdAt)
      tempData.year = date.getFullYear()
      tempData.month = date.getMonth() + 1
      tempData.day = date.getDate()
      // createdAt: "2020-04-28 08:00:00"
      console.log(tempData)
      self.setData({
        meizi: tempData
      })
      self.creatQrcode(tempData.url)
    })
  },
  creatQrcode(text) {
    qrcode = new QRCode('canvas', {
      text,
      width: qrcodeWidth,
      height: qrcodeWidth,
      colorDark: "#1CA4FC",
      colorLight: "white",
      correctLevel: QRCode.CorrectLevel.H,
    });
  },
  onPullDownRefresh () {
    this.getMeizi()
    console.log('onPullDownRefresh')
  },
  // 设置分享
  onShareAppMessage () {
    const self = this
    return {
      title: self.data.meizi.desc,
      imageUrl: self.data.meizi.url
    }
  }
})